﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace SnmpForm.APConfig.DHCP
{
    public partial class ucDhcpConfig : ucAPConfig
    {
        public ucDhcpConfig()
        {
            InitializeComponent();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Update(false);
            if (SyncToAp())
            {
                SaveToDb();
            }
        }

        public override bool Update(bool bView = true)
        {
            DbWrapper.DbDataTypes.Dhcp info = m_ap.GetDhcpConfig();
            if (bView)
            {
                if (info.enable == "enable")
                {
                    radioButtonDhcpEnable.Checked = true;
                }
                else if (info.enable == "disable")
                {
                    radioButtonDhcpDisable.Checked = true;
                }

                string strip = info.ipbegin;
                byte[] byteip = IpByteByStr(strip);
                if (byteip != null)
                {
                    ipAddressControlStart.SetAddressBytes(byteip);
                }

                strip = info.ipend;
                byteip = IpByteByStr(strip);
                if (byteip != null)
                {
                    ipAddressControlEnd.SetAddressBytes(byteip);
                }
                textBoxAddressLease.Text = info.addressLease.ToString();
            }
            else
            {
                if (radioButtonDhcpEnable.Checked)
                {
                    info.enable = "enable";
                }
                else if(radioButtonDhcpDisable.Checked)
                {
                    info.enable = "disable";
                }
                byte[] ip = ipAddressControlStart.GetAddressBytes();
                IPAddress addrIP = new IPAddress(ip);
                if (!addrIP.Equals(ApSearch.IPAddressExtend.ZeroAddress))
                {
                    info.ipbegin = addrIP.ToString();
                }

                ip = ipAddressControlEnd.GetAddressBytes();
                addrIP = new IPAddress(ip);
                if (!addrIP.Equals(ApSearch.IPAddressExtend.ZeroAddress))
                {
                    info.ipend = addrIP.ToString();
                }
                info.addressLease = Convert.ToInt32(textBoxAddressLease.Text);
            }
            return true;
        }

        public override bool SyncToApCommit()
        {
            DbWrapper.DbDataTypes.Dhcp info =
               m_ap.GetDhcpConfig();
            bool bSuccess = m_ap.SetApDhcp(info.enable.ToString());
            return bSuccess;
        }

        public override bool SaveToDb()
        {
            bool bSuccess =  BusinessWrapper.User.Instance.ModifyDhcpConfig(m_ap);
            if (bSuccess)
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
            return bSuccess;
        }

        public override void LoadAp()
        {
            Update();
            //
            bool bPrivilege = BusinessWrapper.User.Instance.Permision(
                BusinessWrapper.Types.Privilege.ManageAP.ApDhcp.Save.permision);
            buttonSave.Visible = bPrivilege;
        }
    }
}
